/*   1:    */ package com.atomikos.icatch.imp;
/*   2:    */ 
/*   3:    */ import com.atomikos.diagnostics.Console;
/*   4:    */ import com.atomikos.icatch.CompositeTransaction;
/*   5:    */ import com.atomikos.icatch.CompositeTransactionManager;
/*   6:    */ import com.atomikos.icatch.Participant;
/*   7:    */ import com.atomikos.icatch.Propagation;
/*   8:    */ import com.atomikos.icatch.SubTxAwareParticipant;
/*   9:    */ import com.atomikos.icatch.SysException;
/*  10:    */ import com.atomikos.icatch.TransactionControl;
/*  11:    */ import com.atomikos.icatch.TxState;
/*  12:    */ import com.atomikos.icatch.system.Configuration;
/*  13:    */ import java.util.Hashtable;
/*  14:    */ import java.util.Properties;
/*  15:    */ import java.util.Stack;
/*  16:    */ 
/*  17:    */ public class BaseTransactionManager
/*  18:    */   implements CompositeTransactionManager, SubTxAwareParticipant
/*  19:    */ {
/*  20:    */   private static final long serialVersionUID = -552994279460833505L;
/*  21: 57 */   private Hashtable threadtotxmap_ = null;
/*  22: 60 */   private Hashtable txtothreadmap_ = null;
/*  23:    */   private TransactionServiceImp service_;
/*  24:    */   private boolean initialized_;
/*  25:    */   
/*  26:    */   protected BaseTransactionManager()
/*  27:    */   {
/*  28: 77 */     this.threadtotxmap_ = new Hashtable();
/*  29: 78 */     this.txtothreadmap_ = new Hashtable();
/*  30: 79 */     this.initialized_ = false;
/*  31:    */   }
/*  32:    */   
/*  33:    */   protected void printMsg(String msg, int level)
/*  34:    */   {
/*  35:    */     try
/*  36:    */     {
/*  37: 91 */       Console console = Configuration.getConsole();
/*  38: 92 */       if (console != null) {
/*  39: 93 */         console.println(msg, level);
/*  40:    */       }
/*  41:    */     }
/*  42:    */     catch (Exception e) {}
/*  43:    */   }
/*  44:    */   
/*  45:    */   private Thread getThread(CompositeTransaction ct)
/*  46:    */   {
/*  47:106 */     Thread thread = null;
/*  48:108 */     synchronized (this.txtothreadmap_)
/*  49:    */     {
/*  50:109 */       thread = (Thread)this.txtothreadmap_.get(ct);
/*  51:    */     }
/*  52:111 */     return thread;
/*  53:    */   }
/*  54:    */   
/*  55:    */   private Stack removeThreadMappings(Thread thread)
/*  56:    */   {
/*  57:123 */     Stack ret = null;
/*  58:124 */     synchronized (this.threadtotxmap_)
/*  59:    */     {
/*  60:125 */       ret = (Stack)this.threadtotxmap_.remove(thread);
/*  61:126 */       CompositeTransaction tx = (CompositeTransaction)ret.peek();
/*  62:127 */       this.txtothreadmap_.remove(tx);
/*  63:    */     }
/*  64:129 */     return ret;
/*  65:    */   }
/*  66:    */   
/*  67:    */   private void setThreadMappings(CompositeTransaction ct, Thread thread)
/*  68:    */     throws IllegalStateException, SysException
/*  69:    */   {
/*  70:144 */     ct.addSubTxAwareParticipant(this);
/*  71:146 */     synchronized (this.threadtotxmap_)
/*  72:    */     {
/*  73:150 */       if (TxState.ACTIVE.equals(ct.getState()))
/*  74:    */       {
/*  75:151 */         Stack txs = (Stack)this.threadtotxmap_.get(thread);
/*  76:152 */         if (txs == null) {
/*  77:153 */           txs = new Stack();
/*  78:    */         }
/*  79:154 */         txs.push(ct);
/*  80:155 */         this.threadtotxmap_.put(thread, txs);
/*  81:156 */         this.txtothreadmap_.put(ct, thread);
/*  82:    */       }
/*  83:    */     }
/*  84:    */   }
/*  85:    */   
/*  86:    */   private void restoreThreadMappings(Stack stack, Thread thread)
/*  87:    */     throws IllegalStateException
/*  88:    */   {
/*  89:167 */     CompositeTransaction tx = (CompositeTransaction)stack.peek();
/*  90:168 */     tx.addSubTxAwareParticipant(this);
/*  91:170 */     synchronized (this.threadtotxmap_)
/*  92:    */     {
/*  93:174 */       Object state = tx.getState();
/*  94:176 */       if ((TxState.ACTIVE.equals(state)) || (TxState.MARKED_ABORT.equals(state)))
/*  95:    */       {
/*  96:178 */         Stack txs = (Stack)this.threadtotxmap_.get(thread);
/*  97:179 */         if (txs != null) {
/*  98:180 */           throw new IllegalStateException("Thread already has subtx stack");
/*  99:    */         }
/* 100:182 */         this.threadtotxmap_.put(thread, stack);
/* 101:183 */         this.txtothreadmap_.put(tx, thread);
/* 102:    */       }
/* 103:    */     }
/* 104:    */   }
/* 105:    */   
/* 106:    */   private CompositeTransactionImp getCurrentTx()
/* 107:    */   {
/* 108:190 */     Thread thread = Thread.currentThread();
/* 109:191 */     synchronized (this.threadtotxmap_)
/* 110:    */     {
/* 111:192 */       Stack txs = (Stack)this.threadtotxmap_.get(thread);
/* 112:193 */       if (txs == null) {
/* 113:194 */         return null;
/* 114:    */       }
/* 115:196 */       return (CompositeTransactionImp)txs.peek();
/* 116:    */     }
/* 117:    */   }
/* 118:    */   
/* 119:    */   public void init(TransactionServiceImp service, Properties properties)
/* 120:    */     throws SysException
/* 121:    */   {
/* 122:216 */     this.service_ = service;
/* 123:217 */     this.service_.init(properties);
/* 124:218 */     this.initialized_ = true;
/* 125:    */   }
/* 126:    */   
/* 127:    */   public Participant getParticipant(String root)
/* 128:    */   {
/* 129:230 */     return this.service_.getParticipant(root);
/* 130:    */   }
/* 131:    */   
/* 132:    */   public void committed(CompositeTransaction tx)
/* 133:    */   {
/* 134:242 */     removeTransaction(tx);
/* 135:    */   }
/* 136:    */   
/* 137:    */   public void rolledback(CompositeTransaction tx)
/* 138:    */   {
/* 139:253 */     removeTransaction(tx);
/* 140:    */   }
/* 141:    */   
/* 142:    */   public CompositeTransaction getCompositeTransaction()
/* 143:    */     throws SysException
/* 144:    */   {
/* 145:263 */     if (!this.initialized_) {
/* 146:264 */       throw new IllegalStateException("Not initialized");
/* 147:    */     }
/* 148:266 */     CompositeTransaction ct = null;
/* 149:267 */     ct = getCurrentTx();
/* 150:268 */     if (ct != null) {
/* 151:269 */       printMsg("getCompositeTransaction()  returning instance with id " + ct.getTid(), 3);
/* 152:    */     } else {
/* 153:272 */       printMsg("getCompositeTransaction() returning NULL!", 3);
/* 154:    */     }
/* 155:274 */     return ct;
/* 156:    */   }
/* 157:    */   
/* 158:    */   public CompositeTransaction getCompositeTransaction(String tid)
/* 159:    */     throws SysException
/* 160:    */   {
/* 161:284 */     CompositeTransaction ret = this.service_.getCompositeTransaction(tid);
/* 162:285 */     if (ret != null) {
/* 163:286 */       printMsg("getCompositeTransaction ( " + tid + " ) returning instance with tid " + ret.getTid(), 3);
/* 164:    */     } else {
/* 165:290 */       printMsg("getCompositeTransaction ( " + tid + " ) returning NULL!", 3);
/* 166:    */     }
/* 167:293 */     return ret;
/* 168:    */   }
/* 169:    */   
/* 170:    */   protected synchronized CompositeTransaction recreateCompositeTransaction(Propagation context, boolean orphancheck, boolean heur_commit)
/* 171:    */     throws SysException
/* 172:    */   {
/* 173:320 */     CompositeTransaction ct = null;
/* 174:    */     
/* 175:    */ 
/* 176:323 */     ct = getCurrentTx();
/* 177:324 */     if (ct != null)
/* 178:    */     {
/* 179:325 */       String msg = "Recreating a transaction with existing transaction: " + ct.getTid();
/* 180:326 */       printMsg(msg, 1);
/* 181:    */     }
/* 182:330 */     ct = this.service_.recreateCompositeTransaction(context, orphancheck, heur_commit);
/* 183:    */     
/* 184:332 */     Thread t = Thread.currentThread();
/* 185:333 */     setThreadMappings(ct, t);
/* 186:334 */     return ct;
/* 187:    */   }
/* 188:    */   
/* 189:    */   public CompositeTransaction suspend()
/* 190:    */     throws SysException
/* 191:    */   {
/* 192:343 */     Thread thread = Thread.currentThread();
/* 193:345 */     if (!this.initialized_) {
/* 194:346 */       throw new IllegalStateException("Not initialized");
/* 195:    */     }
/* 196:348 */     CompositeTransaction ret = getCurrentTx();
/* 197:349 */     if (ret != null)
/* 198:    */     {
/* 199:350 */       printMsg("suspend() for transaction " + ret.getTid(), 2);
/* 200:    */       
/* 201:352 */       removeThreadMappings(thread);
/* 202:    */     }
/* 203:    */     else
/* 204:    */     {
/* 205:354 */       printMsg("suspend() called without a transaction context", 2);
/* 206:    */     }
/* 207:357 */     return ret;
/* 208:    */   }
/* 209:    */   
/* 210:    */   public void resume(CompositeTransaction ct)
/* 211:    */     throws IllegalStateException, SysException
/* 212:    */   {
/* 213:368 */     Thread thread = Thread.currentThread();
/* 214:370 */     if (!this.initialized_) {
/* 215:371 */       throw new IllegalStateException("Not initialized");
/* 216:    */     }
/* 217:375 */     Stack ancestors = new Stack();
/* 218:376 */     Stack tmp = new Stack();
/* 219:377 */     Stack lineage = (Stack)ct.getLineage().clone();
/* 220:378 */     boolean done = false;
/* 221:379 */     while ((!lineage.isEmpty()) && (!done))
/* 222:    */     {
/* 223:380 */       CompositeTransaction parent = (CompositeTransaction)lineage.pop();
/* 224:381 */       if (!parent.isLocal()) {
/* 225:382 */         done = true;
/* 226:    */       } else {
/* 227:384 */         tmp.push(parent);
/* 228:    */       }
/* 229:    */     }
/* 230:387 */     while (!tmp.isEmpty()) {
/* 231:388 */       ancestors.push(tmp.pop());
/* 232:    */     }
/* 233:391 */     ancestors.push(ct);
/* 234:    */     
/* 235:393 */     restoreThreadMappings(ancestors, thread);
/* 236:394 */     printMsg("resume ( " + ct + " ) done for transaction " + ct.getTid(), 2);
/* 237:    */   }
/* 238:    */   
/* 239:    */   public void shutdown(boolean force)
/* 240:    */     throws SysException, IllegalStateException
/* 241:    */   {
/* 242:420 */     this.service_.shutdown(force);
/* 243:    */   }
/* 244:    */   
/* 245:    */   protected void startlistening(CompositeTransaction transaction)
/* 246:    */     throws SysException
/* 247:    */   {
/* 248:426 */     transaction.addSubTxAwareParticipant(this);
/* 249:    */   }
/* 250:    */   
/* 251:    */   private void removeTransaction(CompositeTransaction ct)
/* 252:    */   {
/* 253:440 */     if (ct == null) {
/* 254:441 */       return;
/* 255:    */     }
/* 256:443 */     Thread thread = getThread(ct);
/* 257:444 */     if (thread == null) {
/* 258:445 */       return;
/* 259:    */     }
/* 260:447 */     Stack mappings = removeThreadMappings(thread);
/* 261:448 */     if ((mappings != null) && (!mappings.empty()))
/* 262:    */     {
/* 263:449 */       mappings.pop();
/* 264:450 */       if (!mappings.empty()) {
/* 265:451 */         restoreThreadMappings(mappings, thread);
/* 266:    */       }
/* 267:    */     }
/* 268:    */   }
/* 269:    */   
/* 270:    */   public CompositeTransaction createCompositeTransaction(long timeout)
/* 271:    */     throws SysException
/* 272:    */   {
/* 273:462 */     Stack errors = new Stack();
/* 274:463 */     CompositeTransaction ct = null;CompositeTransaction ret = null;
/* 275:464 */     Thread thread = Thread.currentThread();
/* 276:    */     
/* 277:466 */     ct = getCurrentTx();
/* 278:467 */     if (ct == null)
/* 279:    */     {
/* 280:469 */       ret = this.service_.createCompositeTransaction(timeout);
/* 281:470 */       printMsg("createCompositeTransaction ( " + timeout + " ): " + "created new ROOT transaction with id " + ret.getTid(), 2);
/* 282:    */     }
/* 283:    */     else
/* 284:    */     {
/* 285:475 */       printMsg("createCompositeTransaction ( " + timeout + " )", 2);
/* 286:    */       
/* 287:    */ 
/* 288:478 */       ret = ct.getTransactionControl().createSubTransaction();
/* 289:    */     }
/* 290:481 */     setThreadMappings(ret, thread);
/* 291:    */     
/* 292:483 */     return ret;
/* 293:    */   }
/* 294:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions\3.7.0\transactions-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.imp.BaseTransactionManager
 * JD-Core Version:    0.7.0.1
 */